package com.dgut.activiti.service;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.activiti.entity.ProcessDefinitionDTO;
import com.dgut.common.util.ExtPageable;
@Service
@Transactional
public class ActivitiService implements IActivitiService {
	@Autowired
	private RepositoryService repositoryService;// 提供了管理和控制 流程部署和流程定义的操作

	@Override
	public void depoly() {
	}

	@Override
	public void readResource(String processDefinitionId, String resourceName) {

	}

	@Override
	public Page<ProcessDefinitionDTO> processAjaxList(ExtPageable pageable) {
		// 1.查询所有已部署的流程定义
		List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
		// 2.自定义DTO数据传输对象封装 ProcessDefinition接口集合的数据
		// ，进行json序列化（解决ProcessDefinition接口无法序列化为json问题）
		List<ProcessDefinitionDTO> dtoList = new ArrayList<ProcessDefinitionDTO>();
		for (ProcessDefinition pd : processDefinitionList) {
			ProcessDefinitionDTO dto = new ProcessDefinitionDTO();
			dto.setId(pd.getId());
			dto.setCategory(pd.getCategory());
			dto.setName(pd.getName());
			dto.setKey(pd.getKey());
			dto.setDescription(pd.getDescription());
			dto.setVersion(pd.getVersion());
			dto.setResourceName(pd.getResourceName());
			dto.setDeploymentId(pd.getDeploymentId());
			dto.setDiagramResourceName(pd.getDiagramResourceName());
			dto.setStartFormKey(pd.hasStartFormKey());
			dto.setGraphicalNotation(pd.hasGraphicalNotation());
			dto.setSuspended(pd.isSuspended());
			dto.setTenantId(pd.getTenantId());

			dtoList.add(dto);
		}
		// 3.把流程列表集合封装为Spring分页对象
		Page<ProcessDefinitionDTO> processDefinitionPage = new PageImpl<ProcessDefinitionDTO>(dtoList,
				pageable.getPageable(), processDefinitionList.size());
		return processDefinitionPage;
	}

	@Override
	public void deleteProcessDefinition(String deploymentId) {
		repositoryService.deleteDeployment(deploymentId, true);
	}

}
